package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.srit.core.bean.Page;
import com.srit.forecast.constant.Constant;
import com.srit.system.bean.EffectTimeGroup;
import com.srit.system.dao.EffectTimeDao;
import com.srit.system.dao.EffectTimeGroupDao;

@Service
@Transactional
public class EffectTimeGroupDaoImpl implements EffectTimeGroupDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private EffectTimeDao effectTimeDao;

	@Override
	public void save(EffectTimeGroup effectTimeGroup) {
		String sql = "insert into TC_EFFECTTIMEGROUP (ID,NAME,ORDERNO) VALUES (SEQ_TC_EFFECTTIMEGROUP.nextval,?,?)";
		jdbcTemplate.update(sql, new Object[] { effectTimeGroup.getName(),
				effectTimeGroup.getOrderNo() });
		int id = this.getMaxId();
		effectTimeGroup.setId(id);
	}

	@Override
	public void update(EffectTimeGroup effectTimeGroup) {
		String sql = "UPDATE  TC_EFFECTTIMEGROUP SET NAME=?,ORDERNO=? WHERE ID=?";
		jdbcTemplate.update(sql, new Object[] { effectTimeGroup.getName(),
				effectTimeGroup.getOrderNo(), effectTimeGroup.getId() });

	}

	@Override
	public EffectTimeGroup getById(Integer id) {
		String sql = "select * from TC_EFFECTTIMEGROUP where id=?";
		EffectTimeGroup effectTimeGroup = jdbcTemplate.queryForObject(sql,
				new Object[] { id }, new EffectTimeGroupRowMapper());
		if (effectTimeGroup != null) {
			effectTimeGroup.setEffectTimeList(effectTimeDao.listByGroupId(id));
		}
		return effectTimeGroup;
	}

	@Override
	public List<EffectTimeGroup> listAll() {
		String sql = "select * from TC_EFFECTTIMEGROUP";
		return jdbcTemplate.query(sql, new EffectTimeGroupRowMapper());
	}

	@Override
	public int getMaxId() {
		String sql = "select max(id) from TC_EFFECTTIMEGROUP";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public void page(Page page) {
		String sqlCount = "select count(*) from TC_EFFECTTIMEGROUP where 1=1 ";
		String sqlAnd = " ";
		String sqlSelect = "select * "
				+ "from (select rownum r,t1.* from TC_EFFECTTIMEGROUP t1 where rownum<? "
				+ sqlAnd + ") t2 where t2.r>?" + Constant.SQL_ORDER_BY_ID;
		int count = jdbcTemplate.queryForInt(sqlCount + sqlAnd);
		List<EffectTimeGroup> list = jdbcTemplate.query(sqlSelect,
				new Object[] { (page.getPageNum() + 1) * page.getPageSize(),
						page.getPageNum() * page.getPageSize() },
				new EffectTimeGroupRowMapper());
		page.setCount(count);
		page.setList(list);
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

}

class EffectTimeGroupResultSetExtractor implements ResultSetExtractor {
	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		EffectTimeGroup effectTimeGroup = new EffectTimeGroup();
		effectTimeGroup.setId(rs.getInt("ID"));
		effectTimeGroup.setName(rs.getString("NAME"));
		effectTimeGroup.setOrderNo(rs.getString("ORDERNO"));
		return effectTimeGroup;
	}
}

class EffectTimeGroupRowMapper implements RowMapper {

	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		EffectTimeGroupResultSetExtractor extractor = new EffectTimeGroupResultSetExtractor();
		return extractor.extractData(rs);
	}

}
